package com.atstudying.mall.product.service.impl;

import com.atstudying.common.utils.PageUtils;
import com.atstudying.common.utils.Query;
import com.atstudying.mall.product.dao.BrandDao;
import com.atstudying.mall.product.entity.BrandEntity;
import com.atstudying.mall.product.service.BrandService;
import com.atstudying.mall.product.service.CategoryBrandRelationService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        //Map<String, Object> params中如果传入的参数由key（模糊匹配）
        //select * from pms_attr_group where catelog_id=? and (att_group_id=key or att_group_name like %key%)
        String key= (String) params.get("key");
        //让三级分类的id，等于我传过来的指定值
        QueryWrapper<BrandEntity> wrapper = new QueryWrapper<BrandEntity>();

        if (!StringUtils.isEmpty(key)){
            wrapper.eq("brand_id",key).or().like("name",key);
//            wrapper.and((obj)->{
//                obj.eq("brand_id",key).or().like("name",key);
//            });


        }
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),wrapper

        );

        return new PageUtils(page);
    }

    @Override
    public void updateDetail(BrandEntity brand) {

        //保证冗余字段的数据一致
        //更新自己表
        this.updateById(brand);
        if (!StringUtils.isEmpty(brand.getName())){
            //同步更新其他关联表中的数据
            categoryBrandRelationService.updateBrand(brand.getName(),brand.getBrandId());

            //todo 更新其他关联
        }
    }

}